package cn.aifeix.org.controller;

import cn.aifeix.basic.anno.LogAnno;
import cn.aifeix.basic.util.AjaxResult;
import cn.aifeix.basic.util.PageList;
import cn.aifeix.org.domain.DictionaryDetail;
import cn.aifeix.org.domain.Employee;
import cn.aifeix.org.query.DictionaryDetailQuery;
import cn.aifeix.org.service.DictionaryDetailService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/det")
public class DictionaryDetailController {
    @Autowired
    private DictionaryDetailService dictionaryDetailService;

    /**
     * 添加/修改
     */
    @PutMapping
    @LogAnno(method = "addOrUpdate",methodDes = "添加或修改")
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail){
        try {
            if (null == dictionaryDetail.getId()){
                dictionaryDetailService.add(dictionaryDetail);
            }else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @LogAnno(method = "delete",methodDes = "删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        dictionaryDetailService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 查询所有
     */
    @GetMapping
    @LogAnno(method = "findAll",methodDes = "查询所有")
    public List<DictionaryDetail> findAll(){
        return dictionaryDetailService.findAll();
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("/{id}")
    public DictionaryDetail findById(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id){
        return dictionaryDetailService.findById(id);
    }

    /**
     *
     * 分页查询
     */
    @PostMapping
    public PageList<DictionaryDetail> queryByPage(@RequestBody DictionaryDetailQuery query){
        return dictionaryDetailService.queryByPage(query);
    }

    /**
     * 批量删除
     */
    @PatchMapping
    @LogAnno(method = "batchDel",methodDes = "批量删除")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        dictionaryDetailService.batchDel(ids);
        return AjaxResult.me().success();
    }

    /**
     * 根据类型id查询
     */
    @GetMapping("/findByTypeId/{id}")
    @ApiOperation(value = "通过部门ID查询" )
    public List<DictionaryDetail> findByTypeId(@PathVariable("id") Long typeId){
        return dictionaryDetailService.findByTypeId(typeId);
    }
}
